函数ReadDat()实现从文件ENG.IN中读取一篇英文文章存入到 字符串数组xx中; 请编制函数DelWord()分别按行删除空格、标点 符号以及10个不区分大小写的英文单词(you,for,your,on,no,if, the,in,to,all), 余下的单词按顺序重新存入数组xx中, 最后调用 函数WriteDat()把结果xx输出到文件PS6.OUT中。 例如: 原文: You are a student. 结果: areastudent 原始数据文件存放的格式是:每行的宽度均小于80个字符, 含 标点符号和空格。 注意: 部分源程序存放在PROG1.C中。文章每行中的单词与单 词之间用空格或其它标点符号分隔, 每单词均小于20个字符。 请勿改动主函数main()、读数据函数ReadDat()和输出数据函 数WriteDat()的内容。
/*参考答案*/
#include #include #include #include
char WORD[10][10] = {"you", "for", "your", "on", "no","if","the","in","to","all"} ; char xx[50][80] ; int maxline = 0 ; /* 文章的总行数 */
int ReadDat(void) ; void WriteDat(void) ;
void DelWord(void) { int i,j,k,n,len; char word[20],c; char str[80];
for(i = 0; i < maxline; i++) { len = strlen(xx); memset(str,0,80*sizeof(char));
n = 0; for(j = 0; j < len+1; j++) { c = xx[j];
if((c>='a' && c<='z') || (c>='A' && c<='Z')) { word[n] = c; n++; } else { word[n] = '\0'; if(word[0] != '\0') { for(k = 0; k < 10; k++) if(strcmpi(WORD[k],word) == 0) break;
if(k >= 10) strcat(str,word); } n = 0; } }
len = strlen(str); memcpy(xx,str,len+1); } }
void main() { clrscr() ; if(ReadDat()) { printf("数据文件ENG.IN不能打开!\n\007") ; return ; } DelWord() ; WriteDat() ; }
int ReadDat(void) { FILE *fp ; int i = 0 ; char *p ;
if((fp = fopen("eng.in", "r")) == NULL) return 1 ; while(fgets(xx, 80, fp) != NULL) { p = strchr(xx, '\n') ; if(p) xx[p - xx] = 0 ; i++ ; } maxline = i ; fclose(fp) ; return 0 ; }
void WriteDat(void) { FILE *fp ; int i ;
fp = fopen("ps6.out", "w") ; for(i = 0 ; i < maxline ; i++) { printf("%s\n", xx) ; fprintf(fp, "%s\n", xx) ; } fclose(fp) ;
|